package com.wlc.controller;

import com.wlc.pojo.Role;
import com.wlc.pojo.Result;
import com.wlc.service.RoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/role")
@Slf4j
public class RoleController {

    @Autowired
    private RoleService roleService;

    @GetMapping("/{id}")
    public Result<Role> getById(@PathVariable Long id) {
        log.info("根据ID查询角色: {}", id);
        try {
            Role role = roleService.findById(id);
            return Result.success(role);
        } catch (Exception e) {
            log.error("查询角色失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/list")
    public Result<List<Role>> list() {
        log.info("查询所有角色列表");
        try {
            List<Role> roles = roleService.findAll();
            return Result.success(roles);
        } catch (Exception e) {
            log.error("查询角色列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping
    public Result<String> save(@RequestBody Role role) {
        log.info("新增角色: {}", role.getRoleName());
        try {
            roleService.save(role);
            return Result.success("新增角色成功");
        } catch (Exception e) {
            log.error("新增角色失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PutMapping
    public Result<String> update(@RequestBody Role role) {
        log.info("更新角色: {}", role.getId());
        try {
            roleService.update(role);
            return Result.success("更新角色成功");
        } catch (Exception e) {
            log.error("更新角色失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @DeleteMapping("/{id}")
    public Result<String> delete(@PathVariable Long id) {
        log.info("删除角色: {}", id);
        try {
            roleService.deleteById(id);
            return Result.success("删除角色成功");
        } catch (Exception e) {
            log.error("删除角色失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @PostMapping("/{roleId}/menus")
    public Result<String> assignMenus(@PathVariable Long roleId, @RequestBody List<Long> menuIds) {
        log.info("分配角色菜单: roleId={}", roleId);
        try {
            roleService.assignMenus(roleId, menuIds);
            return Result.success("分配菜单成功");
        } catch (Exception e) {
            log.error("分配角色菜单失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }

    @GetMapping("/{roleId}/menuIds")
    public Result<List<Long>> getMenuIds(@PathVariable Long roleId) {
        log.info("查询角色菜单ID列表: roleId={}", roleId);
        try {
            List<Long> menuIds = roleService.findMenuIdsByRoleId(roleId);
            return Result.success(menuIds);
        } catch (Exception e) {
            log.error("查询角色菜单ID列表失败: {}", e.getMessage());
            return Result.error(e.getMessage());
        }
    }
}